package com.swordxu.yim.server.loginserver;

import com.swordxu.yim.server.tool.IMPacketDispatcher;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.apache.log4j.Logger;

/**
 * 登录服务器handler类.
 * Created by 徐晓明 on 2014/12/23.
 */
public class LoginServerHandler extends SimpleChannelInboundHandler<String> {
    private static final Logger logger = Logger.getLogger(LoginServerHandler.class);

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("有用户连接了");
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
//        ctx.flush();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) {
        //解析处理客户端发送到登录服务器的消息
        System.out.println("收到消息鸟" + ctx.channel().isWritable());
//        ctx.writeAndFlush("神啊");
        System.out.println(msg);
        IMPacketDispatcher.dispatch(msg.toString(), ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

}
